#include<cstring>
#include<cstdio>
using namespace std;
struct str {
	char s[105];
	int len;
};
int n, tail, pl[8005];
char ch[105];
str now, st[8005];
inline bool cmp(str xx, str yy) {
	if (xx.len != yy.len)return 0;
	for (int i = 0 ; i < xx.len ; i++)
		if (xx.s[i] != yy.s[i])return 0;
	return 1;
}
inline int pd(str xx) {
	for (int i = 1 ; i <= tail ; i++)
		if (cmp(xx, st[i]))return pl[i];
	return 0;
}
inline bool check(str xx) {
	int an = 0, c1 = 0, c2 = 0, count = 0, num = 0;
	for (int i = 0 ; i < xx.len ; i++) {
		if (xx.s[i] == '.')c1++;
		else if (xx.s[i] == ':')c2++;
		else if (xx.s[i] >= '0' && xx.s[i] <= '9')continue;
		else return 0;
	}
	if (c1 != 3 || c2 != 1)return 0;
	for (int i = 0 ; i < xx.len ; i++) {
		if (xx.s[i] == '.' || xx.s[i] == ':') {
			if (c1 != 0 && xx.s[i] == ':')return 0;
			c1--;
			if (an > 255)return 0;
			an = 0;
			count = 0;
			continue;
		}
		if (count == 1 && an == 0)return 0;
		an = (an << 3) + (an << 1) + xx.s[i] - '0';
		if (c1 >= 0 && an > 255)return 0;
		else if (c1 < 0 && an > 65535)return 0;
		if (count == 0)num++;
		count++;
	}
	if (num != 5)return 0;
	return 1;
}
int main() {
	freopen("network.in","r",stdin);
	freopen("network.out","w",stdout);
	scanf("%d", &n);
	for (int i = 1 ; i <= n ; i++) {
		scanf("%s", ch);
		scanf("%s", now.s);
		now.len = strlen(now.s);
		if (ch[0] == 'S') {
			if (check(now)) {
				if (pd(now) == 0) {
					st[++tail] = now;
					pl[tail] = i;
					printf("OK\n");
				}
				else printf("FAIL\n");
			}
			else printf("ERR\n");
		}
		else {
			if (check(now)) {
				if (pd(now) == 0)printf("FAIL\n");
				else printf("%d\n", pd(now));
			}
			else printf("ERR\n");
		}
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
}
